<template>
	<div class="devtool-gentable-container">
		<el-dialog title="新增库表" v-model="isShowDialog">
			<el-form ref="addGentableFormRef" :model="formData" :rules="formRules" label-width="100px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="库表名称" prop="tableName">
							<el-input v-model="formData.tableName" placeholder="请输入库表名称,只允许英文字母或下划线组成的2~32个字符" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="库表描述" prop="tableComment">
							<el-input v-model="formData.tableComment" placeholder="请输入库表描述，例如 学生信息表" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="实体类名称" prop="className">
							<el-input v-model="formData.className" placeholder="请输入实体类名称(首字母大写),只允许英文字母组成驼峰式的2~32个字符" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="作者名称" prop="functionAuthor">
							<el-input v-model="formData.functionAuthor" placeholder="请输入作者名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col>
						<el-form-item label="项目包路径" prop="packageName">
							<el-input v-model="formData.packageName" placeholder="请输入项目包路径，例如 com.daffodil.myapp" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="功能描述" prop="functionName">
							<el-input v-model="formData.functionName" placeholder="请输入功能描述，例如 学生信息" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="工程上下文" prop="functionApi">
							<el-input v-model="formData.functionApi" placeholder="工程上下文，例如 myapp"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="模块名称" prop="moduleName">
							<el-input v-model="formData.moduleName" placeholder="请输入模块名称，例如 school" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="业务名称" prop="businessName">
							<el-input v-model="formData.businessName" placeholder="请输入业务名称，例如 student" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col>
						<el-form-item label="备注" prop="remark">
							<el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col>
						<el-descriptions title="项目工程包预览" column="1" border>
							<el-descriptions-item label="Entity">{{formData.packageName || 'com.daffodil.myapp'}}.entity.{{formData.className || 'Student'}}</el-descriptions-item>
							<el-descriptions-item label="Controller">{{formData.packageName || 'com.daffodil.myapp'}}.controller.{{formData.className || 'Student'}}Controller</el-descriptions-item>
							<el-descriptions-item label="Interface">{{formData.packageName || 'com.daffodil.myapp'}}.service.I{{formData.className || 'Student'}}Service</el-descriptions-item>
							<el-descriptions-item label="Service">{{formData.packageName || 'com.daffodil.myapp'}}.service.impl.{{formData.className || 'Student'}}ServiceImpl</el-descriptions-item>
							<el-descriptions-item label="Api-list"><el-tag>GET</el-tag> /api-{{formData.functionApi || 'xxxx'}}/{{formData.moduleName || 'school'}}/{{formData.businessName || 'student'}}/list</el-descriptions-item>
							<el-descriptions-item label="Api-info"><el-tag>GET</el-tag> /api-{{formData.functionApi || 'xxxx'}}/{{formData.moduleName || 'school'}}/{{formData.businessName || 'student'}}/info</el-descriptions-item>
							<el-descriptions-item label="Api-add"><el-tag>POST</el-tag> /api-{{formData.functionApi || 'xxxx'}}/{{formData.moduleName || 'school'}}/{{formData.businessName || 'student'}}/add</el-descriptions-item>
							<el-descriptions-item label="Api-edit"><el-tag>POST</el-tag> /api-{{formData.functionApi || 'xxxx'}}/{{formData.moduleName || 'school'}}/{{formData.businessName || 'student'}}/edit</el-descriptions-item>
							<el-descriptions-item label="Api-remove"><el-tag>POST</el-tag> /api-{{formData.functionApi || 'xxxx'}}/{{formData.moduleName || 'school'}}/{{formData.businessName || 'student'}}/remove</el-descriptions-item>
						</el-descriptions>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { addGentable } from '/@/api/devtool/gentable/index';
export default {
	name: 'devtool/gentable/add',
	setup(props, context) {
		const addGentableFormRef = ref();
		const validTableName = (rule, value, callback) => {
			if(!value){
				return callback(new Error('库表名称不能为空'));
			}
			if(!/^[a-zA-Z][a-zA-Z_]{1,32}$/.test(value)){
				return callback(new Error('只允许英文字母或下划线组成的2~32个字符'));
			}
			callback();
		}
		const validClassName = (rule, value, callback) =>{
			if(!value){
				return callback(new Error('实体类名称不能为空'));
			}
			if(!/^[A-Z][a-zA-Z]{1,32}$/.test(value)){
				return callback(new Error('只允许英文字母(首字母大写)组成驼峰式的2~32个字符'));
			}
			callback();
		}
		const validPackageName = (rule, value, callback) =>{
			if(!value){
				return callback(new Error('项目包路径不能为空'));
			}
			if(!/^[a-zA-Z][a-zA-Z\.]{1,128}$/.test(value)){
				return callback(new Error('只允许英文字母和小圆点分割组成的2~128个字符'));
			}
			callback();
		}
		const validModuleName = (rule, value, callback) => {
			if(!value){
				return callback(new Error('模块名称不能为空'));
			}
			if(!/^[a-zA-Z][a-zA-Z]{1,32}$/.test(value)){
				return callback(new Error('只允许英文字母组成的2~32个字符'));
			}
			callback();
		}
		const validBusinessName = (rule, value, callback) => {
			if(!value){
				return callback(new Error('业务名称不能为空'));
			}
			if(!/^[a-zA-Z][a-zA-Z_]{1,32}$/.test(value)){
				return callback(new Error('只允许英文字母组成的2~32个字符'));
			}
			callback();
		}
		const state = reactive({
			isShowDialog: false,
			formData: {},
			formRules : {
				tableName: { required: true, validator: validTableName, trigger: 'blur' },
				tableComment: { required: true, message: '库表描述不能为空', trigger: 'blur' },
				className: { required: true, validator: validClassName, trigger: 'blur' },
				functionAuthor: { required: true, message: '作者名称不能为空', trigger: 'blur' },
				packageName: { required: true, validator: validPackageName, trigger: 'blur' },
				functionApi: { required: true, message: '工程上下文不能为空', trigger: 'blur' },
				moduleName: { required: true, validator: validModuleName, trigger: 'blur' },
				businessName: { required: true, validator: validBusinessName, trigger: 'blur' },
				functionName: { required: true, message: '功能名称不能为空', trigger: 'blur' },
			}
		});

		// 打开弹窗
		const openDialog = (row?: object) => {
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
			for(var key in state.formData) {
				state.formData[key] = undefined;
			}
			addGentableFormRef.value.resetFields();
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			addGentableFormRef.value.validate(valid => {
				if(valid){
					let params = {
						tableName: state.formData.tableName,
						tableComment: state.formData.tableComment,
						className: state.formData.className,
						functionAuthor: state.formData.functionAuthor,
						packageName: state.formData.packageName,
						functionApi: state.formData.functionApi,
						moduleName: state.formData.moduleName,
						businessName: state.formData.businessName,
						functionName: state.formData.functionName,
						remark: state.formData.remark
					}
					addGentable(params).then((res) => {
						if(res.code === 0){
							ElMessage.success('保存成功');
							context.emit('submit');
							closeDialog();
						}
					});
				}
			});
		};
		return {
			addGentableFormRef,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
};
</script>